package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import ex.com.jm.food.dao.AdminAdsDao;
import ex.com.jm.food.entity.Ads;
/**
 * dao层实现层
 * @author zhoajw
 *
 */
public class AdminAdsDaoImpl extends HibernateDaoSupport implements AdminAdsDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Ads> getAds(final int initPage,final int pageRow,final String adName,final String s_date,
			final String e_date,final Integer type,final Integer status,final String sort,
			final String order) {
		// TODO Auto-generated method stub
		Object ads=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql="select ad.ad_id,ad.ad_name,ad.status,"
						+ "case when ad.ad_type=0 then '特价' when ad.ad_type=1 then '热销' when ad.ad_type=2 then '特价' end,"
						+ "ad.price,"
						+ " ad.created,menu.menu_name,com.com_name,style.style_value,style.style_price "
						+ " from jm_ads as ad left join jm_menu as menu on ad.menu_id=menu.menu_id "
						+ " left join jm_company as com on com.com_id=ad.com_id "
						+ " left join jm_picstyle style on ad.style_id=style.style_id "
						+ " where 1=1 ";
				//String hql="from Ads ads where 1=1 ";
				if(adName!=null&&!adName.equals("")){
					sql+=" and ad.ad_name like '%"+adName+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					sql+=" and ad.created>="+s_date;
				}
				if(e_date!=null&&!e_date.equals("")){
					sql+=" and ad.created <="+e_date;
				}
				if(type!=null&&!type.equals("")){
					sql+=" and ad.ad_type="+type;
				}
				if(status!=null&&!status.equals("")){
					sql+=" and ads.status="+status;
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					sql+=" order by "+order+"  "+sort;
				}
				Query query=session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Ads>) ads;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int adsCount(final String adName,final String s_date,final String e_date,final Integer type,
			final Integer status) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Ads ads where 1=1 ";
				if(adName!=null&&!adName.equals("")){
					hql+=" and ad.adName like '%"+adName+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and ads.created>="+s_date;
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and ads.created <="+e_date;
				}
				if(type!=null&&!type.equals("")){
					hql+=" and ads.adType="+type;
				}
				if(status!=null&&!status.equals("")){
					hql+=" and ads.status="+status;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final Integer status,final String id) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("update Ads ads set ads.status="+status+"where ads.adId="+id);
				int c=query.executeUpdate();
				return c;
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@Override
	public Ads editAds(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Ads.class, id);
	}

}
